package com.demo.cityselect;

import android.app.Activity;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import java.util.ArrayList;
import java.util.List;

/**
 * date:2021/6/18
 * author:wsm(admin)
 * funcation:城市区列表
 */
public class CitySeachListAdapter extends RecyclerView.Adapter<CitySeachListAdapter.ViewHolder> {
    private Activity mActivity;
    private List<CitySeachBean> mList;
    private String mKeyword;

    public CitySeachListAdapter(Activity activity) {
        mActivity = activity;
        mList = new ArrayList<>();
    }

    public void addAll(List<CitySeachBean> result) {
        if (result != null) {
            mList.addAll(result);
            notifyDataSetChanged();
        }
    }

    public void setKeyword(String key) {
        mKeyword = key;
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mActivity).inflate(R.layout.layout_seach_city_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SpannableString search = ReturnUtils.matcherSearchText(mActivity, mList.get(position).getCity(), mKeyword);
        SpannableString searchs = ReturnUtils.matcherSearchText(mActivity, mList.get(position).getText(), mKeyword);
        if (search != null) {
            holder.mName.setText(search);
            holder.cityName.setText(searchs);
        } else {
            holder.mName.setText(mList.get(position).getCity());
            holder.cityName.setText(mList.get(position).getText());
        }
        holder.mName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnClickItemlistener != null) {
                    mOnClickItemlistener.onItem(mList.get(position).getCity(), mList.get(position).getCode());
                }
            }
        });
        holder.cityName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnClickItemlistener != null) {
                    mOnClickItemlistener.onItem(mList.get(position).getText(), mList.get(position).getValue());
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView mName;
        private final TextView cityName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mName = itemView.findViewById(R.id.name);
            cityName = itemView.findViewById(R.id.city_name);
        }
    }

    public interface OnClickItemlistener {
        void onItem(String city, int code);
    }

    public OnClickItemlistener mOnClickItemlistener;

    public void setOnClickItemlistener(OnClickItemlistener onClickItemlistener) {
        mOnClickItemlistener = onClickItemlistener;
    }
}
